<?php
class MChain extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}

	function getprovince(){
		$result = array();
		$this->db->select('*');
		$this->db->from('tbl_province');
		$this->db->order_by('PROVINCE_NAME','ASC');
		$array_keys_values = $this->db->get();
		foreach ($array_keys_values->result() as $row)
		{
			$result[""]= '---- SELECT PROVINCE -----';
			$result[$row->PROVINCE_ID]= $row->PROVINCE_NAME;
		}
	    return $result;
	 }
	 
    
    function get_district($id=""){
        $result = array();
        $this->db->select('*');
        $this->db->from('tbl_district');
        $this->db->where('PROVINCE_ID',$id);
        $this->db->where('district_status',0);
        $this->db->order_by('district_NAME','ASC');
        $array_keys_values = $this->db->get();
        foreach ($array_keys_values->result() as $row)
        {
            $result[0]= '---- SELECT DISTRICT -----';
            $result[$row->district_ID]= $row->district_NAME;
        }
        return $result;
    }
    
    
    function getprovince_user(){
    	$result = array();
    	$this->db->select('*');
    	$this->db->from('tbl_province');
    	$this->db->order_by('PROVINCE_NAME','ASC');
    	$array_keys_values = $this->db->get();
    	foreach ($array_keys_values->result() as $row)
    	{
    		$result[""]= '---- SELECT PROVINCE -----';
    		$result[$row->PROVINCE_ID]= $row->PROVINCE_NAME;
    	}
    	return $result;
    }
    
    
    function getdistrict_user($id=""){
    	$result = array();
    	$this->db->select('*');
    	$this->db->from('tbl_district');
    	$this->db->where('PROVINCE_ID',$id);
    	$this->db->order_by('district_NAME','ASC');
    	$array_keys_values = $this->db->get();
    	foreach ($array_keys_values->result() as $row)
    	{
    		$result[0]= '---- SELECT DISTRICT -----';
    		$result[$row->district_ID]= $row->district_NAME;
    	}
    	return $result;
    }
    
    
    function getSubdistrict($id=""){
    	$res = $this->db->query('select * from tbl_district WHERE PROVINCE_ID='.$id.' AND district_status=0 Order By district_NAME ASC');
    	$str = '<option value="">--- SELECT DISTRICT ---</option>';
    	foreach ($res->result() as $p){
    		$str .='<option value="'.$p->district_ID.'">'.$p->district_NAME.'</option>';
    	}
    	echo $str;
    }
    
    
    public function subprovince()
    {
    	$res = $this->db->query('select * from tbl_province Order By PROVINCE_ID ASC');
    	$str = '<option value="">--- SELECT PROVINCE ---</option>';
    	foreach ($res->result() as $p){
    		$str .='<option value="'.$p->PROVINCE_ID.'">'.$p->PROVINCE_NAME.'</option>';
    	}
    	echo $str;
    }
    
    
    function select_province($id=""){
    	$res = $this->db->query('select * from tbl_province Order By PROVINCE_NAME ASC');
    	$str ='<select name="province">';
    	$str .= '<option value="">--- SELECT PROVINCE ---</option>';
    	foreach ($res->result() as $p){
    		$res1 = $this->db->query('select * from tbl_district WHERE district_ID='.$id.'');
    		foreach ($res1->result() as $d){
	    		if($p->PROVINCE_ID==$d->PROVINCE_ID){
	    			$chk="selected";
	    		}else{
	    			$chk="";
	    		}
    		}
    		$str .='<option value="'.$p->PROVINCE_ID.'" '.$chk.'>'.$p->PROVINCE_NAME.'</option>';
    	}
    	$str .= '</select>';
    	return $str;
    }
    
  
}
?>